package com.yq.cp.controller.permission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.Permission;
import com.yq.cp.entity.permission.UserRole;
import com.yq.cp.entity.permission.dto.PermissionDto;
import com.yq.cp.mapper.permission.UserRoleMapper;
import com.yq.cp.service.permission.PermissionService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Resource
    PermissionService permissionService;
    @Resource
    UserRoleMapper userRoleMapper;
    /** 新增 */
    @PostMapping("add")
    public ApiResult add(@RequestBody Permission permission){
        permission.setCreateUserId(UserThreadLocal.get().getId());
        permissionService.save(permission);
        return  ApiResult.ok();
    }
    /** 修改 */
    @PostMapping("update")
    public ApiResult update(@RequestBody Permission permission){
        permissionService.updateById(permission);
        return  ApiResult.ok();
    }

    /** 查询所有的权限 */
    @PostMapping("queryAll")
    public ApiResult queryAll(@RequestBody PageVo vo){
        List<PermissionDto> permissions = permissionService.queryAll(vo.getId(),vo.getType());
        return  ApiResult.ok().data(permissions);
    }
    /** 根据角色查询已分配的所有的权限 */
    @PostMapping("queryAllByRoleId")
    public ApiResult queryAllByRoleId(@RequestBody Permission permission){
        if (permission.getParentId()==null){
            permission.setParentId(0L);
        }
        List<PermissionDto> permissions = permissionService.queryAllByRoleId(permission.getParentId(), permission.getId(),
                permission.getType());
        return  ApiResult.ok().data(permissions);
    }
    /** 查询所有的权限,与分配情况 */
    @PostMapping("queryAllAndAllot")
    public ApiResult queryAllAndAllot(@RequestBody Permission permission){
        if (permission.getParentId()==null){
            permission.setParentId(0L);
        }
        List<PermissionDto> permissions = permissionService.queryAllAndAllot(permission.getParentId(), permission.getId(),1);
        List<PermissionDto> permissions1 = permissionService.queryAllAndAllot(permission.getParentId(), permission.getId(),2);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("manage",permissions);
        map.put("user",permissions1);
        return  ApiResult.ok().data(map);
    }

    /** 查询登陆人的权限 */
    @PostMapping("/user/all")
    public ApiResult userAll(){
        QueryWrapper<UserRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",UserThreadLocal.get().getId());
        UserRole userRole = userRoleMapper.selectOne(queryWrapper);
        if (userRole!=null){
            List<PermissionDto> permissions = permissionService.queryAllByRoleId(0L, userRole.getRoleId(),2);
            return  ApiResult.ok().data(permissions);
        }else {
            return ApiResult.ok();
        }
    }
}

